package com.centriole.servicepaasuser.dao;

import com.centriole.common.entity.PaasApp;
import com.centriole.common.entity.PaasService;
import com.centriole.common.entity.PaasUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

@Mapper
public interface PaasUserMapper {

    /**
     * 新增 (Paas内部管理)用户
     * @param paasUser  paasUser对象
     * @return
     */
    @Insert("INSERT INTO paas_user(userId,accountName,password,nickName,status) VALUES(#{u.userId},#{u.accountName},#{u.password},#{u.nickName},1)")
    @SelectKey(statement="select replace(UUID(),'-','') as userId", keyProperty="u.userId", before=true, statementType= StatementType.STATEMENT,resultType=String.class)
    int insertPaasUser(@Param("u") PaasUser paasUser);

    /**
     * 新增 (Paas内部管理)应用
     * @param paasApp  paasApp对象
     * @return
     */
    @Insert("INSERT INTO paas_app(appId,appName,appStatus) VALUES(#{p.appId},#{p.appName},1)")
    @SelectKey(statement="select replace(UUID(),'-','') as appId", keyProperty="p.appId", before=true, statementType= StatementType.STATEMENT,resultType=String.class)
    int insertPaasApp(@Param("p") PaasApp paasApp);

    /**
     * 新增 (Paas内部管理)服务
     * @param paasService  paasService对象
     * @return
     */
    @Insert("INSERT INTO paas_service(sId,serviceName,serviceId,serviceStatus) VALUES(#{s.sId},#{s.serviceName},#{s.serviceId},1)")
    @SelectKey(statement="select replace(UUID(),'-','') as sId", keyProperty="s.sId", before=true, statementType= StatementType.STATEMENT,resultType=String.class)
    int insertPaasService(@Param("s") PaasService paasService);

    /**
     * 更新(Paas内部管理)用户
     * @param paasUser
     * @return
     */
    int updatePaasUser(@Param("u") PaasUser paasUser);

    /**
     * 更新(Paas内部管理)应用
     * @param paasApp
     * @return
     */
    int updatePaasApp(@Param("p") PaasApp paasApp);

    /**
     * 更新(Paas内部管理)服务
     * @param paasService
     * @return
     */
    int updatePaasService(@Param("s") PaasService paasService);

    /**
     * 查询所有用户(不包含其 拥有可使用服务的集合)
     * @return  List<PaasUser>
     */
    @Select("SELECT * FROM paas_user")
    List<PaasUser> selectAllPaasUsers();

    /**
     * 关联查询所有用户(包含其 拥有可使用服务的集合)
     * @return  List<PaasUser>
     */
    List<PaasUser> selectRAllPaasUsers();

    /**
     * 关联查询所有服务(包含 可以对其访问的用户)
     * @return  List<PaasUser>
     */
    List<PaasService> selectRAllPaasServices();

    /**
     * 查询所有状态有效的应用
     * @return     List<PaasApp>
     */
    @Select("SELECT * FROM paas_app WHERE appStatus = 1")
    List<PaasApp> selectAllPaasApps();

    /**
     * 查询所有服务
     * @return     List<PaasApp>
     */
    @Select("SELECT * FROM paas_service")
    List<PaasService> selectAllPaasServices();

    /**
     * 指定userId查询用户
     * @param userId    String 32位UUID
     * @return  PaasUser对象
     */
    @Select("SELECT * FROM paas_user WHERE userId = #{userId}")
    PaasUser selectPaasUserByUserId(@Param("userId") String userId);

    /**
     * 指定userId查询用户(包含其 拥有可使用服务的集合)
     * @param  userId 32位UUID
     * @return  PaasUser对象
     */
    PaasUser selectRPaasUserByUserId(@Param("userId") String userId);

    /**
     * 指定userId查询用户(包含 可以对其访问的用户)
     * @param  sId 32位UUID
     * @return  PaasService对象
     */
    PaasService selectRPaasServiceBySid(@Param("sId") String sId);

    /**
     * 根据accountName(最左匹配原则)模糊查询用户
     * @param accountName    用户账号名
     * @return  PaasUser对象
     */
    @Select("SELECT * FROM paas_user WHERE accountName LIKE '${accountName}%' ")
    List<PaasUser> selectPaasUsersLikeAccountName(@Param("accountName") String accountName);

    /**
     * 给指定用户关联服务
     * @param userId    32位UUID
     * @param sId   32位UUID
     * @return
     */
    @Insert("insert into paas_user_service(userId,sId) values(#{userId},#{sId})")
    int relatePaasUserAndService(@Param("userId") String userId, @Param("sId") String sId);


}
